package com.ucode.tool.util;

import java.util.List;

/**
 * List 物理分页
 * @author: liliang
 * @date: 2020年1月31日 上午11:21:58
 */
public class ListPageModel {

    private int page = 1; // 当前页
    public int totalPages = 0; // 总页数
    private int pageSize;// 每页5条数据
    private int totalRows = 0; // 总数据数
    private int pageStartRow = 0;// 每页的起始数
    private int pageEndRow = 0; // 每页显示数据的终止数
    private boolean hasNextPage = false; // 是否有下一页
    private boolean hasPreviousPage = false; // 是否有前一页
    private List list;

    public ListPageModel(List list, int pageSize) {
        init(list, pageSize);// 通过对象集，记录总数划分
    }

    /**
     * 初始化list，并告之该list每页的记录数
     * 
     * @author liliang
     * @date 2020年1月31日
     * @param list
     * @param pageSize
     */
    public void init(List list, int pageSize) {
        this.pageSize = pageSize;
        this.list = list;
        totalRows = list.size();
        // it = list.iterator();
        hasPreviousPage = false;
        if ((totalRows % pageSize) == 0) {
            totalPages = totalRows / pageSize;
        } else {
            totalPages = totalRows / pageSize + 1;
        }

        if (page >= totalPages) {
            hasNextPage = false;
        } else {
            hasNextPage = true;
        }

        if (totalRows < pageSize) {
            this.pageStartRow = 0;
            this.pageEndRow = totalRows;
        } else {
            this.pageStartRow = 0;
            this.pageEndRow = pageSize;
        }
    }

    /**
     * 判断要不要分页
     * 
     * @author liliang
     * @date 2020年1月31日
     * @return
     */
    public boolean isNext() {
        return list.size() > 5;
    }
    
    public String description() {
        String description = "共有数据数:" + this.getTotalRows()
                + "共有页数: " + this.getTotalPages()
                + "当前页数为:" + this.getPage()
                + " 是否有前一页: " + this.isHasPreviousPage()
                + " 是否有下一页:" + this.isHasNextPage()
                + " 开始行数:" + this.getPageStartRow()
                + " 终止行数:" + this.getPageEndRow();

        return description;
    }

    /**
     * 获取下一页数据
     * @author liliang
     * @date 2020年1月31日
     * @return
     */
    public List getNextPage() {
        page = page + 1;
        disposePage();
        this.description();
        return getObjects(page);
    }

    
    private void disposePage() {
        if (page == 0) {
            page = 1;
        }

        if ((page - 1) > 0) {
            hasPreviousPage = true;
        } else {
            hasPreviousPage = false;
        }

        if (page >= totalPages) {
            hasNextPage = false;
        } else {
            hasNextPage = true;
        }
    }

    /**
     * 获取上一页数据
     * @author liliang
     * @date 2020年1月31日
     * @return
     */
    public List getPreviousPage() {
        page = page - 1;

        if ((page - 1) > 0) {
            hasPreviousPage = true;
        } else {
            hasPreviousPage = false;
        }
        if (page >= totalPages) {
            hasNextPage = false;
        } else {
            hasNextPage = true;
        }
        this.description();
        return getObjects(page);
    }

    /**
     * 获取第几页的内容
     * @param page
     * @return
     */
    public List getObjects(int page) {
        if (page == 0) {
            this.setPage(1);
        } else {
            this.setPage(page);
        }
        this.disposePage();
        if (page * pageSize < totalRows) {// 判断是否为最后一页
            pageEndRow = page * pageSize;
            pageStartRow = pageEndRow - pageSize;
        } else {
            pageEndRow = totalRows;
            pageStartRow = pageSize * (totalPages - 1);
        }

        List objects = null;
        if (!list.isEmpty()) {
            objects = list.subList(pageStartRow, pageEndRow);
        }
        return objects;
    }

    /**
     * 获取第一页数据
     * @author liliang
     * @date 2020年1月31日
     * @return
     */
    public List getFistPage() {
        if (this.isNext()) {
            return list.subList(0, pageSize);
        } else {
            return list;
        }
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(int totalPages) {
        this.totalPages = totalPages;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getTotalRows() {
        return totalRows;
    }

    public void setTotalRows(int totalRows) {
        this.totalRows = totalRows;
    }

    public int getPageStartRow() {
        return pageStartRow;
    }

    public void setPageStartRow(int pageStartRow) {
        this.pageStartRow = pageStartRow;
    }

    public int getPageEndRow() {
        return pageEndRow;
    }

    public void setPageEndRow(int pageEndRow) {
        this.pageEndRow = pageEndRow;
    }

    public boolean isHasNextPage() {
        return hasNextPage;
    }

    public void setHasNextPage(boolean hasNextPage) {
        this.hasNextPage = hasNextPage;
    }

    public boolean isHasPreviousPage() {
        return hasPreviousPage;
    }

    public void setHasPreviousPage(boolean hasPreviousPage) {
        this.hasPreviousPage = hasPreviousPage;
    }

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }
}
